﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HotelMatchUtility.Comparer;
using HotelMatchUtility.Entity;
using HotelMatchUtility.Data;

namespace HotelMatchUtility.Match
{
    public class MatchEntity
    {
        public static int ExactLeftNowNumber;
        public static int ExactHitNumber;
        public static int SimLeftNowNumber;
        public static int SimHitNumber;
        private string MatchName;

        public MatchEntity(string MatchName)
        {
            this.MatchName = MatchName;
        }
        /// <summary>
        /// 左侧目标 酒店列表
        /// </summary>
        public List<HotelEntity> LeftTargetEntityList;

        /// <summary>
        /// 右侧
        /// </summary>
        public List<HotelEntity> RightToCompareEntityList;

        /// <summary>
        /// 精确匹配条件
        /// </summary>
        public List<ICompareCondition> ExactConditionList;

        /// <summary>
        /// 模糊匹配条件
        /// </summary>
        public List<ICompareCondition> SimilarConditionList;

        /// <summary>
        /// match结果List
        /// </summary>
        public List<HotelEntity> ResultList;

        public string GetMatchName()
        {
            return MatchName;
        }

        public void DoMatch()
        {
            ExactLeftNowNumber = 0;
            ExactHitNumber = 0;
            SimLeftNowNumber = 0;
            SimHitNumber = 0;

            #region 精确匹配
            if (ExactConditionList != null && ExactConditionList.Count > 0)
            {
                foreach (var leftTarget in LeftTargetEntityList)
                {
                    ExactLeftNowNumber++;
                    bool isExactMatched = false;
                    foreach (var rightToCompare in RightToCompareEntityList)
                    {
                        bool targetContinue = false;
                        //如果前N次比较已经排他比对成功
                        if (!rightToCompare.IsExclusive)
                        {
                            //精确匹配
                            foreach (var condition in ExactConditionList)
                            {
                                if (condition.DoCompare(leftTarget, rightToCompare))
                                {
                                    isExactMatched = true;

                                    if (leftTarget.ExactMatchResult.Count > 0
                                        && leftTarget.ExactMatchResult.Last().Hotel == rightToCompare.Hotel)
                                    {
                                        leftTarget.ExactMatchResult.Last().MatchType += "[且]" + condition.GetName();
                                    }
                                    else
                                    {
                                        var copyRightEntity = (HotelEntity)rightToCompare.Clone();
                                        copyRightEntity.MatchType = condition.GetName();
                                        leftTarget.ExactMatchResult.Add(copyRightEntity);
                                    }
                                    if (condition.GetIsExclusiveCondition())
                                    {
                                        rightToCompare.IsExclusive = true;
                                    }
                                    if (condition.GetTargetIsContinue())
                                    {
                                        targetContinue = true;
                                        //break;
                                    }
                                }
                            }
                        }
                        if (targetContinue)
                            break;
                    }
                    if (isExactMatched)
                    {
                        ExactHitNumber++;
                        ResultList.Add(leftTarget);
                    }
                }
            }
            #endregion
            
            #region 相似匹配
            if (SimilarConditionList == null || SimilarConditionList.Count <= 0) return;
            foreach (var leftTarget in LeftTargetEntityList)
            {
                SimLeftNowNumber++;
                bool isSimMatched = false;
                foreach (var rightToCompare in RightToCompareEntityList)
                {
                    bool targetContinue = false;
                    if (!rightToCompare.IsExclusive)
                    {
                        foreach (var condition in SimilarConditionList)
                        {
                            if (condition.DoCompare(leftTarget, rightToCompare))
                            {
                                isSimMatched = true;
                                if (leftTarget.SimilarMatchResult.Count > 0
                                    && leftTarget.SimilarMatchResult.Last().Hotel == rightToCompare.Hotel)
                                {
                                    leftTarget.SimilarMatchResult.Last().MatchType += "[且]" + condition.GetName();
                                }
                                else
                                {
                                    var copyRightEntity = (HotelEntity)rightToCompare.Clone();
                                    copyRightEntity.MatchType = condition.GetName();
                                    if (leftTarget.SimilarMatchResult.Count < 5) //相似最多5个，多了就不加了
                                    {
                                        leftTarget.SimilarMatchResult.Add(copyRightEntity);
                                    }
                                }
                                if (condition.GetIsExclusiveCondition())
                                {
                                    rightToCompare.IsExclusive = true;
                                }
                                if (condition.GetTargetIsContinue())
                                {
                                    targetContinue = true;
                                    //break;
                                }
                            }
                            if (targetContinue)
                                break;
                        }
                    }
                }
                if (isSimMatched)
                {
                    SimHitNumber++;
                    if(ResultList.IndexOf(leftTarget)<0)
                        ResultList.Add(leftTarget);
                }
            }
            #endregion
        }

        /// <summary>
        /// 设置下一个Match
        /// </summary>
        public MatchEntity NextMatch;
    }
}
